//Init system

//used to init the system

#include "types.h"

/******************************************************************************
 * Initialize Timer
 *
 * 
 *Initialize a Timer and configure Channel 0 to Output capture mode
 * to generate 100ms delays 
 *
 * 
 * Input -  None
 * Output - None
 *****************************************************************************/
void InitializeTimer(void)
{

  // Set the timer prescaler to %32, since the bus clock is at 2 MHz,
  // and we want the timer running at 62.5 kHz (= 1/16 MHz)
  TSCR2_PR0 = 1;
  TSCR2_PR1 = 0;
  TSCR2_PR2 = 1;
  
  // Initialize output compare channel 1 to toggle on successful compare.
  TCTL2_OM1 = 0;
  TCTL2_OL1 = 1;
  
  // Enable output compare on Channel 1
  TIOS_IOS1 = 1;
  
  // Set TC1 output compare register to be 0x7A12 (= 31250, or half a second)
  TC1H = 0x7A12;
  
  // Enable output compare interrupts
  // TIE_C1I = 1;
  
  //
  // Enable the timer
  // 
  TSCR1_TEN = 1;
  
  //
  // Set Timer Control Register 4 to detect rising edges only
  //
  TCTL4_EDG1A = 1;
  TCTL4_EDG1B = 0;
}


void InitializePWM(void)
{
   //set LED ports also 
    DDRA_BIT1 = Set;
	DDRA_BIT1 = Set;
	DDRA_BIT1 = Set;
   //Pulse Width Channel 0&1 Enable
   PWME_PWME0 = Set;
   PWME_PWME1 = Set;

  //Set PWM channel 0&1 output to high at the beginning of the period, then goes low when the duty count is reached.
   PWMPOL_PPOL0 = Set;
   PWMPOL_PPOL1 = Set;
   
  //Clock A is the clock source for PWM channel 0&1
   PWMCLK_PCLK0 = Reset;
   PWMCLK_PCLK1 = Reset;
   
  //Prescalar value for bus clock / 128
   PWMPRCLK_PCKA2 = Set;
   PWMPRCLK_PCKA1 = Set;
   PWMPRCLK_PCKA0 = Set;

  //Set Channel 0&1 operates in Left Aligned Output Mode.
   PWMCAE_CAE0 = Reset;
   PWMCAE_CAE1 = Reset;
   
  //Initialize period and duty cycle registers;
   PWMPER0 = PeriodRegisterValue;
   PWMDTY0 = Reset;
   PWMPER1 = PeriodRegisterValue;
   PWMDTY1 = Reset;
    
}

/******************************************************************************
 * InitializeInterrupts
 *
 * This routine Initializes the serial interrupt and enables the interrupts for the program
 *
 * 
 * Input -  None
 * Output - None
 *****************************************************************************/
void InitializeInterrupts(void)
{
	//Enable Receiver Full Interrupt Enable Bit 
     SCI0CR2_RIE = Set;
   //Enable interrupts
     EnableInterrupts;
}


 /******************************************************************************
 * DisableInterrupt
 *
 * This routine Disables the serial interrupt and disbalesthe global interrupt for the program
 *
 * 
 * Input -  None
 * Output - None
 *****************************************************************************/
void DisableInterrupt(void)
{
	//Disable Receiver Full Interrupt Enable Bit 
     SCI0CR2_RIE = Reset;
   //Disable interrupts
     DisableInterrupts;
}

 